package cn.iocoder.yudao.module.ao.controller.admin.classlocation;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ao.controller.admin.classlocation.vo.*;
import cn.iocoder.yudao.module.ao.dal.dataobject.classlocation.ClassLocationDO;
import cn.iocoder.yudao.module.ao.service.classlocation.ClassLocationService;

@Tag(name = "管理后台 - 上课场地信息")
@RestController
@RequestMapping("/ao/class-location")
@Validated
public class ClassLocationController {

    @Resource
    private ClassLocationService classLocationService;

    @PostMapping("/create")
    @Operation(summary = "创建上课场地信息")
    @PreAuthorize("@ss.hasPermission('ao:class-location:create')")
    public CommonResult<Long> createClassLocation(@Valid @RequestBody ClassLocationSaveReqVO createReqVO) {
        return success(classLocationService.createClassLocation(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新上课场地信息")
    @PreAuthorize("@ss.hasPermission('ao:class-location:update')")
    public CommonResult<Boolean> updateClassLocation(@Valid @RequestBody ClassLocationSaveReqVO updateReqVO) {
        classLocationService.updateClassLocation(updateReqVO);
        return success(true);
    }

    // 删除功能已移除，防止误删重要的上课场地基础数据
    // @DeleteMapping("/delete")
    // @Operation(summary = "删除上课场地信息")
    // @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('ao:class-location:delete')")
    // public CommonResult<Boolean> deleteClassLocation(@RequestParam("id") Long id) {
    //     classLocationService.deleteClassLocation(id);
    //     return success(true);
    // }

    // @DeleteMapping("/delete-list")
    // @Parameter(name = "ids", description = "编号", required = true)
    // @Operation(summary = "批量删除上课场地信息")
    // @PreAuthorize("@ss.hasPermission('ao:class-location:delete')")
    // public CommonResult<Boolean> deleteClassLocationList(@RequestParam("ids") List<Long> ids) {
    //     classLocationService.deleteClassLocationListByIds(ids);
    //     return success(true);
    // }

    @GetMapping("/get")
    @Operation(summary = "获得上课场地信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ao:class-location:query')")
    public CommonResult<ClassLocationRespVO> getClassLocation(@RequestParam("id") Long id) {
        ClassLocationDO classLocation = classLocationService.getClassLocation(id);
        return success(BeanUtils.toBean(classLocation, ClassLocationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得上课场地信息分页")
    @PreAuthorize("@ss.hasPermission('ao:class-location:query')")
    public CommonResult<PageResult<ClassLocationRespVO>> getClassLocationPage(@Valid ClassLocationPageReqVO pageReqVO) {
        PageResult<ClassLocationDO> pageResult = classLocationService.getClassLocationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ClassLocationRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出上课场地信息 Excel")
    @PreAuthorize("@ss.hasPermission('ao:class-location:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportClassLocationExcel(@Valid ClassLocationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ClassLocationDO> list = classLocationService.getClassLocationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "上课场地信息.xls", "数据", ClassLocationRespVO.class,
                        BeanUtils.toBean(list, ClassLocationRespVO.class));
    }

}